package org.esisar.controller.album;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.esisar.controller.utils.SessionHelper;
import org.esisar.dao.AlbumStatementCreator;
import org.esisar.dao.JDBCDAO;
import org.esisar.dao.PictureOfAlbumStatementCreator;
import org.esisar.dao.PictureStatementCreator;
import org.esisar.model.Album;
import org.esisar.model.Model;
import org.esisar.model.Picture;
import org.esisar.model.PictureOfAlbum;

public class AlbumController extends HttpServlet {

	JDBCDAO jdbc = new JDBCDAO();

	public void init(ServletConfig config) throws ServletException{
		super.init(config);

	}

	public void doGet(HttpServletRequest req, HttpServletResponse res) 
			throws ServletException, IOException{

		res.setContentType("text/html");

		if(req.getParameter("addAlbum") != null){
			addAlbum(req,res);

		}
		if(req.getParameter("role") != null){
			try {
				fowardToAddPhoto(req,res);
			} catch (SQLException e) {
				e.printStackTrace();
			}	
		}
		if(req.getParameter("addPhoto") != null){
			addPhotoToAlbum(req,res);
		}
		if(req.getParameter("showAlbum") != null){
			try {
				showAlbum(req,res);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	private void showAlbum(HttpServletRequest req, HttpServletResponse res) throws SQLException, ServletException, IOException {
		String idAlbum  = req.getParameter("idAlbum"); 

		Album album = (Album) jdbc.find(new AlbumStatementCreator(), "idAlbum", idAlbum);

		List<Model> pictures = jdbc.findAll(new PictureOfAlbumStatementCreator(), "idAlbum", idAlbum);

		req.setAttribute("album", album);
		req.setAttribute("pictures", pictures);
		req.setAttribute("photoNumber", 0);
		RequestDispatcher dispatcher = req.getRequestDispatcher("/Album/getPhotoOfAlbum.jsp");
		dispatcher.forward(req, res);
	}

	public void addPhotoToAlbum(HttpServletRequest req, HttpServletResponse res) {
		String idPicture = req.getParameter("idPicture");
		String idAlbum = req.getParameter("idAlbum");
		String title = req.getParameter("title");
		String comments = req.getParameter("comments");
		long position = 1;

		PictureOfAlbum pictureOfAlbum = new PictureOfAlbum(Long.parseLong(idAlbum), Long.parseLong(idPicture),position+"", title, comments); //TODO change the possition in the future

		jdbc.addToDB(new PictureOfAlbumStatementCreator(), pictureOfAlbum);
	}

	private void fowardToAddPhoto(HttpServletRequest req, HttpServletResponse res) throws SQLException, ServletException, IOException {
		Long idClient = SessionHelper.getUserId(req);

		List<Model> albums = jdbc.findAll(new AlbumStatementCreator(), "idClient", idClient+"");

		List<Model> pictures = jdbc.findAll(new PictureStatementCreator(), "idClient", idClient+""); //TODO - pictureOfAlbum instead of picture !!

		req.setAttribute("albums", albums);
		req.setAttribute("pictures", pictures);

		RequestDispatcher dispatcher = req.getRequestDispatcher("/Album/addPhotoToAlbum.jsp");
		dispatcher.forward(req, res);




	}

	private void addAlbum(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		String title = req.getParameter("title");
		String subTitle = req.getParameter("subtitle");
		String sharedStr = req.getParameter("shared");
		boolean shared = mapToBoolean(sharedStr);
		long idClient = SessionHelper.getUserId(req);


		Album album = new Album(idClient,title, subTitle, shared);

		jdbc.addToDB(new AlbumStatementCreator(), album);

		req.setAttribute("message", "Album created");
		RequestDispatcher dispatcher = req.getRequestDispatcher("/User/panel.jsp");
		dispatcher.forward(req, res);

	}

	private boolean mapToBoolean(String sharedStr) {
		if (sharedStr == null || !sharedStr.equals("on")){
			return false;
		}
		return true;
	}

}
